module base {
	/**
	 * 游戏服务器全局模块
	 *
	 */
    export class GameRoomGlobalModule extends how.module.GlobalModule
    {
        public static response: any = 
        {
            backToHall: "backToHall",
            onStatusChange: CMDConfig.GET_STATUS_CHANGE,//玩家状态改变
        };
        
        private token: string = "";//验证信息
        private host: string = "";//访问host
        private port: string = "";//访问端口

        public start(): void 
        {
        }

        public backToHall(): void
        {
            this.moduleManager.initModule(MainSceneModule, MainSceneView, MainSceneData);
        }
        
        //获取最后登录服务器的ip和端口号,并连接该服务器
        private getLastServerInfo(): void 
        {
            for (var i = 0; i < AppData.getInstance().roomList.length; i++) 
            {
                var room: RoomData = AppData.getInstance().roomList[i]
                if (AppData.getInstance().lastServerId == room.id) 
                {
                    AppData.getInstance().currentRoom = room;
                    break;
                }
            }
            if (AppData.getInstance().currentGame && AppData.getInstance().currentRoom) 
            {
                switch (AppData.getInstance().currentGame.id) 
                {
                    case DataConfig.KIND_ID_ZJH://诈金花
                        this.moduleManager.initGlobalModule(base.GameSceneGlobalModule, ZJHGameData);//游戏数据模块
                        break;
                    // case DataConfig.KIND_ID_DZPK://德州扑克
                        // this.moduleManager.initGlobalModule(base.GameSceneGlobalModule, NewGameSceneData);//游戏数据模块
                        // break;
                }
                this.communicate(base.ChangeServerGlobalModule.response.changeServer, AppData.getInstance().currentRoom, false, (flag) => {
                    AppData.getInstance().lastServerId = 0;
                    this.enterGameCompLete(flag);
                });
            }
        }

        //进入游戏服务器完成
        private enterGameCompLete(flag: boolean): void 
        {
            // this.hallSocket.close();
            this.communicate(LoaddingGlobalModule.response.hideLoad);
            switch (AppData.getInstance().currentGame.id) 
            {
                case DataConfig.KIND_ID_NN://牛牛
                    console.log(">>>> DataConfig.KIND_ID_NN");
                    break;
                case DataConfig.KIND_ID_ZJH://扎金花
                    //椅子ID为空，返回游戏大厅
                    if (AppData.getInstance().userData.chairID == 65535) 
                    {
                        this.hallSocket.send(CMDConfig.SEND_LEAVE_GAME, {});
                        this.hallSocket.send(CMDConfig.SEND_LEAVE_ROOM, {});
                        if (egret.is(how.Application.currentScene, "ZJHGameSceneView")) 
                        {//如果当前场景为游戏场景
                            this.communicate(BaseGameSceneModule.response.updataBackStatus, BackStatusConfig.back);
                        }
                        return;
                    }
                    if (!egret.is(how.Application.currentScene, "ZJHGameSceneView"))
                    {
                        this.moduleManager.initModule(ZJHGameModule, ZJHGameView, ZJHGameData);
                    } 
                    else 
                    {
                        this.communicate(ZJHGameModule.response.initPlayers);//初始化玩家列表
                        how.SoundManager.stopMusic();
                        how.SoundManager.playMusic("dzpk_bg_mp3");
                    }
                    break;
            }
        }

        //当玩家状态改变
        private onStatusChange(data: any): void 
        {
            //当玩家离开
            if (data.U == AppData.getInstance().userData.id && data.S == UserStatus.NULL)
            {
                AppData.getInstance().lastServerId = 0;
                AppData.getInstance().lastGameId = 0;
                switch (AppData.getInstance().currentGame.id)
                {
                    // case DataConfig.KIND_ID_DZPK://德州扑克
                    //     if (!egret.is(how.Application.currentScene, "NewGameSceneView")) 
                    //     {//德州
                    //         this.communicate(base.ChangeServerGlobalModule.response.changeServer, null, true, () => 
                    //         {
                    //             this.moduleManager.initModule(DZPKRoomHallSceneModule, DZPKRoomHallSceneView, DZPKRoomHallSceneData);
                    //         });
                    //     }
                    //     break;
                    // case DataConfig.KIND_ID_NN://牛牛
                    //     if (!egret.is(how.Application.currentScene, "NNGameSceneView")) 
                    //     {
                    //         this.communicate(base.ChangeServerGlobalModule.response.changeServer, null, true, 
                    //            () => { this.moduleManager.initModule(NNRoomHallSceneModule, NNRoomHallSceneView);}
                    //         );
                    //     }
                    //     break;
                    //  case DataConfig.KIND_ID_ZJH://扎金花
                    //     if (!egret.is(how.Application.currentScene, "ZJHGameSceneView")) 
                    //     {
                    //         this.communicate(base.ChangeServerGlobalModule.response.changeServer, null, true, () => {
                    //             this.moduleManager.initModule(ZJHRoomHallSceneModule, ZJHRoomHallSceneView);
                    //         });
                    //     }
                    //     break;
                }
            }
        }
    }
}

window["base"] || (window["base"] = {});
window["base"]["GameRoomGlobalModule"] || (window["base"]["GameRoomGlobalModule"] = base.GameRoomGlobalModule);
